<h1 id="Room"><a href="#Room" class="headerlink" title="Room"></a>Room</h1><p>一个代表了你的单位和建筑所在房间的对象。
它可以被用来“环顾四周”，查找路径等。所有 <code>RoomObject</code> 都有一个链接到其所在房间 <code>Room</code> 实例的属性 <code>room</code>。</p>
<h2 id="Room.controller" class="api-property api-property--property  "><span class="api-property__name">controller</span><span class="api-property__type"><a href="#StructureController">StructureController</a></span></h2>
<p>该房间中的控制器（Controller）建筑，如果其不存在则返回 undefined。</p>
<h2 id="Room.energyAvailable" class="api-property api-property--property  "><span class="api-property__name">energyAvailable</span><span class="api-property__type">number</span></h2>
<p>本房间中所有 spawn 和 extension 中的可用能量总额。</p>
<h2 id="Room.energyCapacityAvailable" class="api-property api-property--property  "><span class="api-property__name">energyCapacityAvailable</span><span class="api-property__type">number</span></h2>
<p>本房间中所有 spawn 和 extension 的容量上限 <code>energyCapacity</code> 总额。</p>
<h2 id="Room.memory" class="api-property api-property--property  "><span class="api-property__name">memory</span><span class="api-property__type">any</span></h2>
<pre class="highlight javascript tab-javascript "><code>room<span class="token punctuation">.</span>memory<span class="token punctuation">.</span>stage <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span></code></pre><p><code>Memory.rooms[room.name]</code> 的简写。你可以用它来快速访问到该房间特定的内存数据对象。<a href="/global-objects.html#Memory-object">点此了解有关内存的更多信息</a>。</p>
<h2 id="Room.name" class="api-property api-property--property  "><span class="api-property__name">name</span><span class="api-property__type">string</span></h2>
<p>房间名称。</p>
<h2 id="Room.storage" class="api-property api-property--property  "><span class="api-property__name">storage</span><span class="api-property__type"><a href="#StructureStorage">StructureStorage</a></span></h2>
<p>该房间中的 Storage 建筑，如果其不存在则返回 undefined。</p>
<h2 id="Room.terminal" class="api-property api-property--property  "><span class="api-property__name">terminal</span><span class="api-property__type"><a href="#StructureTerminal">StructureTerminal</a></span></h2>
<p>该房间中的 Terminal 建筑，如果其不存在则返回 undefined。</p>
<h2 id="Room.visual" class="api-property api-property--property  "><span class="api-property__name">visual</span><span class="api-property__type"><a href="#RoomVisual">RoomVisual</a></span></h2>
<p>该房间的 <a href="#RoomVisual">RoomVisual</a> 对象。您可以使用该对象在房间中绘制简单的形状 (线条，圆，文本标签)。</p>
<h2 id="Room.serializePath" class="api-property api-property--method  "><span class="api-property__name">Room.serializePath</span><span class="api-property__args">(path)</span>
        <div class="api-property__cpu api-property__cpu--1" title="该方法具有较低的CPU开销。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> path <span class="token operator">=</span> spawn<span class="token punctuation">.</span>pos<span class="token punctuation">.</span><span class="token function">findPathTo</span><span class="token punctuation">(</span>source<span class="token punctuation">)</span><span class="token punctuation">;</span>
Memory<span class="token punctuation">.</span>path <span class="token operator">=</span> Room<span class="token punctuation">.</span><span class="token function">serializePath</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
creep<span class="token punctuation">.</span><span class="token function">moveByPath</span><span class="token punctuation">(</span>Memory<span class="token punctuation">.</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>将路径数组序列化为适合存储在内存中的短字符串形式。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>path</code></td><td>array</td><td><p><code><a href="#Room.findPath">Room.findPath</a></code> 返回的路径数组。</p>
</td>
</tr></tbody></table>

<h3 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h3><p>参数路径的序列化字符串。</p>
<h2 id="Room.deserializePath" class="api-property api-property--method  "><span class="api-property__name">Room.deserializePath</span><span class="api-property__args">(path)</span>
        <div class="api-property__cpu api-property__cpu--1" title="该方法具有较低的CPU开销。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> path <span class="token operator">=</span> Room<span class="token punctuation">.</span><span class="token function">deserializePath</span><span class="token punctuation">(</span>Memory<span class="token punctuation">.</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
creep<span class="token punctuation">.</span><span class="token function">moveByPath</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>将短字符串形式的路径反序列化为路径数组。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>path</code></td><td>string</td><td><p>一个序列化路径字符串。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h3><p>一个路径数组</p>
<h2 id="Room.createConstructionSite" class="api-property api-property--method  "><span class="api-property__name">createConstructionSite</span><span class="api-property__args">(x, y, structureType, [name])<br>(pos, structureType, [name])</span>
        <div class="api-property__cpu api-property__cpu--A" title="这个方法是一个改变游戏状态的动作。在返回OK代码的情况下，它的自然成本增加了0.2个CPU成本。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code>Game<span class="token punctuation">.</span>rooms<span class="token punctuation">.</span>sim<span class="token punctuation">.</span><span class="token function">createConstructionSite</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token constant">STRUCTURE_ROAD</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre class="highlight javascript tab-javascript "><code>Game<span class="token punctuation">.</span>rooms<span class="token punctuation">.</span>sim<span class="token punctuation">.</span><span class="token function">createConstructionSite</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token constant">STRUCTURE_SPAWN</span><span class="token punctuation">,</span>
    <span class="token string">'MySpawn2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在指定位置创建一个新的 <a href="#ConstructionSite">ConstructionSite</a>。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>x</code></td><td>number</td><td><p>X 坐标。</p>
</td>
</tr><tr><td><code>y</code></td><td>number</td><td><p>Y 坐标。</p>
</td>
</tr><tr><td><code>pos</code></td><td>object</td><td><p>可以为 <a href="#RoomPosition">RoomPosition</a>  对象或任何包含 <a href="#RoomPosition">RoomPosition</a> 的对象。</p>
</td>
</tr><tr><td><code>structureType</code></td><td>string</td><td><p><code>STRUCTURE_*</code> 常量之一。</p>
</td>
</tr><tr><td><code>name (可选)</code></td><td>string</td><td><p>建筑的名称，该建筑必须支持设置名字（当前仅有 spawn）。最长不能超过 100 个字符。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h3><p>如下错误码之一：
<table class="api-return-codes"><thead><tr><th>constant</th><th>value</th><th>description</th></tr></thead><tbody>
<tr><td><code>OK</code></td><td>0</td><td><p>这个操作已经成功纳入计划。</p>
</td></tr>
<tr><td><code>ERR_NOT_OWNER</code></td><td>-1</td><td><p>该房间被敌对玩家占领（claim）或预定（reserve）。</p>
</td></tr>
<tr><td><code>ERR_INVALID_TARGET</code></td><td>-7</td><td><p>T该建筑无法被放置在指定位置。</p>
</td></tr>
<tr><td><code>ERR_FULL</code></td><td>-8</td><td><p>你已经放置了太多建筑工地。其上限为 100。</p>
</td></tr>
<tr><td><code>ERR_INVALID_ARGS</code></td><td>-10</td><td><p>不正确的位置。</p>
</td></tr>
<tr><td><code>ERR_RCL_NOT_ENOUGH</code></td><td>-14</td><td><p>房间控制器级别不足。<a href="/control.html">了解更多</a></p>
</td></tr>
</tbody></table>
</p>
<h2 id="Room.createFlag" class="api-property api-property--method  "><span class="api-property__name">createFlag</span><span class="api-property__args">(x, y, [name], [color], [secondaryColor])<br>(pos, [name], [color], [secondaryColor])</span>
        <div class="api-property__cpu api-property__cpu--A" title="这个方法是一个改变游戏状态的动作。在返回OK代码的情况下，它的自然成本增加了0.2个CPU成本。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code>Game<span class="token punctuation">.</span>rooms<span class="token punctuation">.</span>sim<span class="token punctuation">.</span><span class="token function">createFlag</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token string">'Flag1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在指定位置创建一个新的 <a href="#Flag">Flag</a>。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>x</code></td><td>number</td><td><p>X 坐标。</p>
</td>
</tr><tr><td><code>y</code></td><td>number</td><td><p>Y 坐标。</p>
</td>
</tr><tr><td><code>pos</code></td><td>object</td><td><p>可以为 <a href="#RoomPosition">RoomPosition</a>  对象或任何包含 <a href="#RoomPosition">RoomPosition</a> 的对象。</p>
</td>
</tr><tr><td><code>name (可选)</code></td><td>string</td><td><p>新旗帜的名称。它应该是唯一的，即 <code>Game.flags</code> 不应该包含拥有相同名称（哈希键）的其他旗帜。如果未定义，则会生成随机名称。最长不得超过 100 字符。</p>
</td>
</tr><tr><td><code>color (可选)</code></td><td>string</td><td><p>新旗帜的颜色。应为 <code>COLOR_*</code> 常量之一。默认值为 <code>COLOR_WHITE</code>。</p>
</td>
</tr><tr><td><code>secondaryColor (可选)</code></td><td>string</td><td><p>新旗帜的次要颜色。应为 <code>COLOR_*</code> 常量之一。默认值等于 <code>color</code> 属性值。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h3><p>新旗帜的名称，或者下列错误码之一：
<table class="api-return-codes"><thead><tr><th>constant</th><th>value</th><th>description</th></tr></thead><tbody>
<tr><td><code>ERR_NAME_EXISTS</code></td><td>-3</td><td><p>该名称已被现有的旗帜使用。</p>
</td></tr>
<tr><td><code>ERR_FULL</code></td><td>-8</td><td><p>你放置了太多旗帜，每个玩家最多允许放置 10000 个旗帜。</p>
</td></tr>
<tr><td><code>ERR_INVALID_ARGS</code></td><td>-10</td><td><p>位置、名称或者颜色不正确。</p>
</td></tr>
</tbody></table>
</p>
<h2 id="Room.find" class="api-property api-property--method  "><span class="api-property__name">find</span><span class="api-property__args">(type, [opts])</span>
        <div class="api-property__cpu api-property__cpu--2" title="该方法的CPU开销中等。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> targets <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token constant">FIND_DROPPED_RESOURCES</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>targets<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    creep<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>targets<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    creep<span class="token punctuation">.</span><span class="token function">pickup</span><span class="token punctuation">(</span>targets<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> extensions <span class="token operator">=</span> Game<span class="token punctuation">.</span>spawns<span class="token punctuation">[</span><span class="token string">'Spawn1'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token constant">FIND_MY_STRUCTURES</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    filter<span class="token punctuation">:</span> <span class="token punctuation">{</span> structureType<span class="token punctuation">:</span> <span class="token constant">STRUCTURE_EXTENSION</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Spawn has '</span><span class="token operator">+</span>extensions<span class="token punctuation">.</span>length<span class="token operator">+</span><span class="token string">' extensions available'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> targets <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token constant">FIND_HOSTILE_CREEPS</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token function-variable function">filter</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">object</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> object<span class="token punctuation">.</span><span class="token function">getActiveBodyparts</span><span class="token punctuation">(</span><span class="token constant">ATTACK</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>查找房间中指定类型的所有对象。在应用自定义的 filter 之前，搜索结果会被自动缓存到指定的房间和类型，自动缓存将持续到本 tick 结束。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>type</code></td><td>number</td><td><p><code>FIND_*</code> 常量之一。</p>
</td>
</tr><tr><td><code>opts (可选)</code></td><td>object</td><td><p>包含下列可选项的对象：
                    <ul>
                        <li>
                            <div class="api-arg-title">filter</div>
                            <div class="api-arg-type">object, function, string</div>
                            <div class="api-arg-desc">将会使用 <a href="https://lodash.com/docs#filter" target="_blank" rel="noopener">Lodash.filter</a> 方法对结果列表进行筛选。</div>
                        </li>
                    </ul></p>
</td>
</tr></tbody></table>

<h3 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h3><p>找到的对象数组</p>
<table>
<thead>
<tr>
<th>常量</th>
<th>类型</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>FIND_EXIT_TOP</code></td>
<td>RoomPosition</td>
<td>位于房间顶部的出口位置。</td>
</tr>
<tr>
<td><code>FIND_EXIT_RIGHT</code></td>
<td>RoomPosition</td>
<td>位于房间右侧的出口位置。</td>
</tr>
<tr>
<td><code>FIND_EXIT_BOTTOM</code></td>
<td>RoomPosition</td>
<td>位于房间底部的出口位置。</td>
</tr>
<tr>
<td><code>FIND_EXIT_LEFT</code></td>
<td>RoomPosition</td>
<td>位于房间左侧的出口位置。</td>
</tr>
<tr>
<td><code>FIND_EXIT</code></td>
<td>RoomPosition</td>
<td>所有出口位置。</td>
</tr>
<tr>
<td><code>FIND_CREEPS</code></td>
<td>Creep</td>
<td>所有 creep。</td>
</tr>
<tr>
<td><code>FIND_MY_CREEPS</code></td>
<td>Creep</td>
<td>所有属于您的 creep。</td>
</tr>
<tr>
<td><code>FIND_HOSTILE_CREEPS</code></td>
<td>Creep</td>
<td>所有不属于您的 creep。</td>
</tr>
<tr>
<td><code>FIND_SOURCES_ACTIVE</code></td>
<td>Source</td>
<td>仍有能量的 source。</td>
</tr>
<tr>
<td><code>FIND_SOURCES</code></td>
<td>Source</td>
<td>所有 source。</td>
</tr>
<tr>
<td><code>FIND_DROPPED_RESOURCES</code></td>
<td>Resource</td>
<td>所有掉落的资源。</td>
</tr>
<tr>
<td><code>FIND_STRUCTURES</code></td>
<td>Structure</td>
<td>所有建筑。</td>
</tr>
<tr>
<td><code>FIND_MY_STRUCTURES</code></td>
<td>Structure</td>
<td>所有属于您的建筑，不包含中立建筑。</td>
</tr>
<tr>
<td><code>FIND_HOSTILE_STRUCTURES</code></td>
<td>Structure</td>
<td>所有其他玩家的建筑，不包含中立建筑。</td>
</tr>
<tr>
<td><code>FIND_FLAGS</code></td>
<td>Flag</td>
<td>所有旗帜。</td>
</tr>
<tr>
<td><code>FIND_MY_SPAWNS</code></td>
<td>StructureSpawn</td>
<td>所有属于您的 spawn。</td>
</tr>
<tr>
<td><code>FIND_HOSTILE_SPAWNS</code></td>
<td>StructureSpawn</td>
<td>所有其他玩家的 spawn。</td>
</tr>
<tr>
<td><code>FIND_CONSTRUCTION_SITES</code></td>
<td>ConstructionSite</td>
<td>所有建筑工地。</td>
</tr>
<tr>
<td><code>FIND_MY_CONSTRUCTION_SITES</code></td>
<td>ConstructionSite</td>
<td>所有属于您的建筑工地。</td>
</tr>
<tr>
<td><code>FIND_HOSTILE_CONSTRUCTION_SITES</code></td>
<td>ConstructionSite</td>
<td>所有其他玩家的建筑工地。</td>
</tr>
<tr>
<td><code>FIND_MINERALS</code></td>
<td>Mineral</td>
<td>所有矿床。</td>
</tr>
<tr>
<td><code>FIND_NUKES</code></td>
<td>Nuke</td>
<td>所有将落地的核弹。</td>
</tr>
<tr>
<td><code>FIND_TOMBSTONES</code></td>
<td>Tombstone</td>
<td>所有墓碑。</td>
</tr>
<tr>
<td><code>FIND_RUINS</code></td>
<td>Ruin</td>
<td>所有废墟。</td>
</tr>
</tbody>
</table>
<h2 id="Room.findExitTo" class="api-property api-property--method  "><span class="api-property__name">findExitTo</span><span class="api-property__args">(room)</span>
        <div class="api-property__cpu api-property__cpu--3" title="这种方法的CPU成本很高。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> exitDir <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">findExitTo</span><span class="token punctuation">(</span>anotherCreep<span class="token punctuation">.</span>room<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> exit <span class="token operator">=</span> creep<span class="token punctuation">.</span>pos<span class="token punctuation">.</span><span class="token function">findClosestByRange</span><span class="token punctuation">(</span>exitDir<span class="token punctuation">)</span><span class="token punctuation">;</span>
creep<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>exit<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 或简写为：</span>
creep<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>anotherCreep<span class="token punctuation">)</span><span class="token punctuation">;</span>
creep<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RoomPosition</span><span class="token punctuation">(</span><span class="token number">25</span><span class="token punctuation">,</span><span class="token number">25</span><span class="token punctuation">,</span> anotherCreep<span class="token punctuation">.</span>pos<span class="token punctuation">.</span>roomName<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>找到通往另一个房间的出口方向。请注意，房间之间的移动不需要此方法，您只需将另一个房间中的目标传递给 <a href="#Creep.moveTo"><code>Creep.moveTo</code></a> 方法即可。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>room</code></td><td>string, <a href="#Room">Room</a></td><td><p>其他房间的名称或者房间对象。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h3><p>出口方向常量，下列之一：</p>
<ul>
<li><code>FIND_EXIT_TOP</code></li>
<li><code>FIND_EXIT_RIGHT</code></li>
<li><code>FIND_EXIT_BOTTOM</code></li>
<li><code>FIND_EXIT_LEFT</code></li>
</ul>
<p>或者下列错误码之一：
<table class="api-return-codes"><thead><tr><th>constant</th><th>value</th><th>description</th></tr></thead><tbody>
<tr><td><code>ERR_NO_PATH</code></td><td>-2</td><td><p>无法找到路径。</p>
</td></tr>
<tr><td><code>ERR_INVALID_ARGS</code></td><td>-10</td><td><p>不正确的位置。</p>
</td></tr>
</tbody></table>
</p>
<h2 id="Room.findPath" class="api-property api-property--method  "><span class="api-property__name">findPath</span><span class="api-property__args">(fromPos, toPos, [opts])</span>
        <div class="api-property__cpu api-property__cpu--3" title="这种方法的CPU成本很高。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> path <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">findPath</span><span class="token punctuation">(</span>creep<span class="token punctuation">.</span>pos<span class="token punctuation">,</span> targetPos<span class="token punctuation">)</span><span class="token punctuation">;</span>
creep<span class="token punctuation">.</span><span class="token function">move</span><span class="token punctuation">(</span>path<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>direction<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre class="highlight javascript tab-javascript "><code>PathFinder<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">findPath</span><span class="token punctuation">(</span>creep<span class="token punctuation">.</span>pos<span class="token punctuation">,</span> targetPos<span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token function-variable function">costCallback</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">roomName<span class="token punctuation">,</span> costMatrix</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>roomName <span class="token operator">==</span> <span class="token string">'W1N5'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 将其他 creep 所处位置设置为可通过</span>
            costMatrix<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>anotherCreep<span class="token punctuation">.</span>pos<span class="token punctuation">.</span>x<span class="token punctuation">,</span> anotherCreep<span class="token punctuation">.</span>pos<span class="token punctuation">.</span>y<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 将旗帜的位置设置为障碍</span>
            costMatrix<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>flag<span class="token punctuation">.</span>pos<span class="token punctuation">.</span>x<span class="token punctuation">,</span> flag<span class="token punctuation">.</span>pos<span class="token punctuation">.</span>y<span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 将位置 (25,20) 的移动成本设置为 50</span>
            costMatrix<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><pre class="highlight javascript tab-javascript "><code><span class="token keyword">let</span> path <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">findPath</span><span class="token punctuation">(</span>creep<span class="token punctuation">.</span>pos<span class="token punctuation">,</span> targetPos<span class="token punctuation">,</span> <span class="token punctuation">{</span>maxOps<span class="token punctuation">:</span> <span class="token number">200</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token operator">!</span>path<span class="token punctuation">.</span>length <span class="token operator">||</span> <span class="token operator">!</span>targetPos<span class="token punctuation">.</span><span class="token function">isEqualTo</span><span class="token punctuation">(</span>path<span class="token punctuation">[</span>path<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
    path <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">findPath</span><span class="token punctuation">(</span>creep<span class="token punctuation">.</span>pos<span class="token punctuation">,</span> targetPos<span class="token punctuation">,</span> <span class="token punctuation">{</span>
        maxOps<span class="token punctuation">:</span> <span class="token number">1000</span><span class="token punctuation">,</span> ignoreDestructibleStructures<span class="token punctuation">:</span> <span class="token boolean">true</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">if</span><span class="token punctuation">(</span> path<span class="token punctuation">.</span>length <span class="token punctuation">)</span> <span class="token punctuation">{</span>
    creep<span class="token punctuation">.</span><span class="token function">move</span><span class="token punctuation">(</span>path<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>direction<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>使用跳点搜索算法 (<a href="http://en.wikipedia.org/wiki/Jump_point_search" target="_blank">Jump Point Search</a>) 在 fromPos 和 toPos 之间找到房间内的最佳路径。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>fromPos</code></td><td><a href="#RoomPosition">RoomPosition</a></td><td><p>起始位置。</p>
</td>
</tr><tr><td><code>toPos</code></td><td><a href="#RoomPosition">RoomPosition</a></td><td><p>结束位置。</p>
</td>
</tr><tr><td><code>opts (可选)</code></td><td>object</td><td><p>包含下列寻路可选项的对象：</p>
<ul>
    <li>
        <div class="api-arg-title">ignoreCreeps</div>
        <div class="api-arg-type">boolean</div>
        <div class="api-arg-desc">将其他 creep 所处的地块视作可通行的。在附近有大量移动的 creep 或者其他一些情况时会很有用。默认值为 false。</div>
    </li>
    <li>
        <div class="api-arg-title">ignoreDestructibleStructures</div>
        <div class="api-arg-type">boolean</div>
        <div class="api-arg-desc">将可破坏的建筑 (constructed walls, ramparts, spawns, extensions) 所在的地块视作可通行的。默认为 false。</div>
    </li>
    <li>
        <div class="api-arg-title">ignoreRoads</div>
        <div class="api-arg-type">boolean</div>
        <div class="api-arg-desc">无视道路。启用该项将加快搜索速度。默认值为 false。仅当新的 <a href="#PathFinder"><code>PathFinder</code></a> 启用时才可用。</div>
    </li>
    <li>
        <div class="api-arg-title">costCallback</div>
        <div class="api-arg-type">function(string, CostMatrix)</div>
        <div class="api-arg-desc">你可以使用该回调在搜索过程中为任意房间修改 <a href="#PathFinder-CostMatrix"><code>CostMatrix</code></a>。回调接受两个参数，<code>roomName</code> 和 <code>costMatrix</code>。使用 <code>costMatrix</code> 实例来修改地形移动成本。如果你从回调中返回了一个新的 matrix。它将会代替内置的缓存 matrix。仅当新的 <a href="#PathFinder"><code>PathFinder</code></a> 启用时才可用。</div>
    </li>
    <li>
        <div class="api-arg-title">ignore</div>
        <div class="api-arg-type">array</div>
        <div class="api-arg-desc">一个数组，其元素为房间中的对象或者 <a href="#RoomPosition">RoomPosition</a> 对象，在搜索时会将该数组中的对象位置视作可通行的地块。当启用新的 <a href="#PathFinder"><code>PathFinder</code></a> 时无法使用。（请用 <code>costCallback</code> 选项代替）。</div>
    </li>
    <li>
        <div class="api-arg-title">avoid</div>
        <div class="api-arg-type">array</div>
        <div class="api-arg-desc">一个数组，其元素为房间中的对象或者 <a href="#RoomPosition">RoomPosition</a> 对象，在搜索时会将该数组中的对象位置视作无法通行的地块。当启用新的 <a href="#PathFinder"><code>PathFinder</code></a> 时无法使用。（请用 <code>costCallback</code> 选项代替）。</div>
    </li>
    <li>
        <div class="api-arg-title">maxOps</div>
        <div class="api-arg-type">number</div>
        <div class="api-arg-desc">用于寻路的消耗上限。你可以限制在寻路上花费的 CPU 时间，基于 1 op ~ 0.001 CPU 的比例。默认值为 2000。</div>
    </li>
    <li>
        <div class="api-arg-title">heuristicWeight</div>
        <div class="api-arg-type">number</div>
        <div class="api-arg-desc">应用于 A* 算法 <code>F = G + weight * H</code> 中的启发式权重(weight)。在使用该选项之前您最好已经了解了 A* 算法的底层实现！默认值为 1.2。</div>
    </li>
    <li>
        <div class="api-arg-title">serialize</div>
        <div class="api-arg-type">boolean</div>
        <div class="api-arg-desc">如果为 true，将会使用 <code><a href="#Room.serializePath">Room.serializePath</a></code> 对结果路径进行序列化。默认值为 false。</div>
    </li>
    <li>
        <div class="api-arg-title">maxRooms</div>
        <div class="api-arg-type">number</div>
        <div class="api-arg-desc">寻路所允许的最大房间数。默认值为 16。仅当新的 <a href="#PathFinder"><code>PathFinder</code></a> 启用时才可用。</div>
    </li>
    <li>
        <div class="api-arg-title">range</div>
        <div class="api-arg-type">number</div>
        <div class="api-arg-desc">找到到达位于目标指定线性区域内位置的路径。默认值为 0.</div>
    </li>
    <li>
        <div class="api-arg-title">plainCost</div>
        <div class="api-arg-type">number</div>
        <div class="api-arg-desc">平原地形的移动成本。默认为 1。</div>
    </li>
    <li>
        <div class="api-arg-title">swampCost</div>
        <div class="api-arg-type">number</div>
        <div class="api-arg-desc">沼泽地形的移动成本。默认为 5。</div>
    </li>
</ul></td>
</tr></tbody></table>

<h3 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h3><p>一个数组，其元素为如下形式的路径步骤：</p>
<pre class="highlight javascript tab-javascript nosidebar"><code><span class="token punctuation">[</span>
    <span class="token punctuation">{</span> x<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span> dx<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>  dy<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> direction<span class="token punctuation">:</span> <span class="token constant">RIGHT</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> x<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">6</span><span class="token punctuation">,</span> dx<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>  dy<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> direction<span class="token punctuation">:</span> <span class="token constant">BOTTOM</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> x<span class="token punctuation">:</span> <span class="token number">9</span><span class="token punctuation">,</span>  y<span class="token punctuation">:</span> <span class="token number">7</span><span class="token punctuation">,</span> dx<span class="token punctuation">:</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> dy<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> direction<span class="token punctuation">:</span> <span class="token constant">BOTTOM_LEFT</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token operator">...</span>
<span class="token punctuation">]</span></code></pre><h2 id="Room.getEventLog" class="api-property api-property--method  "><span class="api-property__name">getEventLog</span><span class="api-property__args">([raw])</span>
        <div class="api-property__cpu api-property__cpu--1" title="该方法具有较低的CPU开销。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token comment">// 追踪特定 creep 执行的事件</span>
_<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">getEventLog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>objectId<span class="token punctuation">:</span> creep<span class="token punctuation">.</span>id<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre class="highlight javascript tab-javascript "><code><span class="token comment">// 查找针对您的 creep 和建筑的所有敌对行动</span>
_<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>Game<span class="token punctuation">.</span>rooms<span class="token punctuation">,</span> <span class="token parameter">room</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> eventLog <span class="token operator">=</span> room<span class="token punctuation">.</span><span class="token function">getEventLog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> attackEvents <span class="token operator">=</span> _<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>eventLog<span class="token punctuation">,</span> <span class="token punctuation">{</span>event<span class="token punctuation">:</span> <span class="token constant">EVENT_ATTACK</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    attackEvents<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">event</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> target <span class="token operator">=</span> Game<span class="token punctuation">.</span><span class="token function">getObjectById</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>data<span class="token punctuation">.</span>targetId<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>target <span class="token operator">&amp;&amp;</span> target<span class="token punctuation">.</span>my<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>event<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>返回该房间中前一个 tick 发生的事件数组。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>raw</code></td><td>boolean</td><td><p>如果该参数为 false 或者未定义，则本方法将会返回使用 <code>JSON.parse</code> 解析后的对象，在首次访问时可能会造成一些 CPU 消耗（返回值会被缓存以方便后续调用）。如果 <code>raw</code> 为 true。则原始的 JSON 字符串将会被返回。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h3><p>事件数组。每个事件都代表一个游戏动作，其格式如下：</p>
<pre class="highlight javascript tab-javascript nosidebar"><code><span class="token punctuation">{</span>
    event<span class="token punctuation">:</span> <span class="token constant">EVENT_ATTACK</span><span class="token punctuation">,</span>
    objectId<span class="token punctuation">:</span> <span class="token string">'54bff72ab32a10f73a57d017'</span><span class="token punctuation">,</span>
    data<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token comment">/* ... */</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>不同类型事件的 <code>data</code> 属性都是不相同的，详见下表：</p>
<table>
    <tr>
        <th>事件</th><th>介绍</th>
    </tr>
    <tr>
        <td><code>EVENT_ATTACK</code></td>
        <td>
            一个 creep 或者建筑攻击了另一个对象。
            <ul>
                <li><code>targetId</code> - 目标对象 ID</li>
                <li><code>damage</code> - 造成的 hit 伤害量</li>
                <li><code>attackType</code> - 下列常量之一：
                    <ul>
                        <li><code>EVENT_ATTACK_TYPE_MELEE</code> - creep 使用 <a href="#Creep.attack">attack</a> 进行了攻击</li>
                        <li><code>EVENT_ATTACK_TYPE_RANGED</code> - creep 使用 <a href="#Creep.rangedAttack">rangedAttack</a> 进行了攻击，或者 tower 使用 <a href="#StructureTower.attack">attack</a> 进行了攻击</li>
                        <li><code>EVENT_ATTACK_TYPE_RANGED_MASS</code> - creep 使用 <a href="#Creep.rangedMassAttack">rangedMassAttack</a> 进行了攻击</li>
                        <li><code>EVENT_ATTACK_TYPE_DISMANTLE</code> - creep 使用 <a href="#Creep.dismantle">dismantle</a> 进行了攻击</li>
                        <li><code>EVENT_ATTACK_TYPE_HIT_BACK</code> - creep 反击了其他 creep 的 <a href="#Creep.attack">attack</a> 攻击</li>
                        <li><code>EVENT_ATTACK_TYPE_NUKE</code> - 核弹着陆</li>
                    </ul>
                </li></ul>
        </td>
    </tr>
    <tr>
        <td><code>EVENT_OBJECT_DESTROYED</code></td>
        <td>
            一个游戏对象被摧毁或是被消灭。
            <ul><li><code>type</code> - 被摧毁对象的类型</li></ul>
        </td>
    </tr>
    <tr>
        <td><code>EVENT_ATTACK_CONTROLLER</code></td>
        <td>一个 creep 在该房间执行了 <a href="#Creep.attackController"><code>attackController</code></a></td>
    </tr>
    <tr>
        <td><code>EVENT_BUILD</code></td>
        <td>
            一个 creep 在该房间执行了 <a href="#Creep.build"><code>build</code></a>。
            <ul>
                <li><code>targetId</code> - 目标对象的 ID</li>
                <li><code>amount</code> - 取得的建造进度</li>
                <li><code>energySpent</code> - 此次行动消耗的能量</li></ul>
        </td>
    </tr>
    <tr>
        <td><code>EVENT_HARVEST</code></td>
        <td>
            一个 creep 在该房间执行了 <a href="#Creep.harvest"><code>harvest</code></a>。
            <ul>
                <li><code>targetId</code> - 目标对象的 ID</li>
                <li><code>amount</code> - 资源采集量</li></ul>
        </td>
    </tr>
    <tr>
        <td><code>EVENT_HEAL</code></td>
        <td>
            一个 creep 或者 tower 治疗了另一个 creep。
            <ul>
                <li><code>targetId</code> - 目标对象的 ID</li>
                <li><code>amount</code> - hit 治疗量</li>
                <li><code>healType</code> - 下列常量之一:
                    <ul>
                        <li><code>EVENT_HEAL_TYPE_MELEE</code> - 一个 creep 使用 <a href="#Creep.heal">heal</a> 进行了治疗</li>
                        <li><code>EVENT_HEAL_TYPE_RANGED</code> - 一个 creep 使用 <a href="#Creep.rangedHeal">rangedHeal</a>进行了治疗，或者一个 tower 使用 <a href="#StructureTower.heal">heal</a> 进行了治疗</li>
                    </ul>
                </li></ul>
        </td>
    </tr>
    <tr>
        <td><code>EVENT_REPAIR</code></td>
        <td>
            一个 creep 或者 tower 修复了建筑。
            <ul>
                <li><code>targetId</code> - 目标对象 ID</li>
                <li><code>amount</code> - hit 修复量</li>
                <li><code>energySpent</code> - 此次行动消耗的能量</li></ul>
            
        </td>
    </tr>
    <tr>
        <td><code>EVENT_RESERVE_CONTROLLER</code></td>
        <td>
            一个 creep 在该房间执行了 <a href="#Creep.reserveController"><code>reserveController</code></a>。
            <ul>
                <li><code>amount</code> - 取得的预定时间</li></ul>
        </td>
    </tr>
    <tr>
        <td><code>EVENT_UPGRADE_CONTROLLER</code></td>
        <td>
            一个 creep 在该房间执行了 <a href="#Creep.upgradeController"><code>upgradeController</code></a>。
            <ul>
                <li><code>amount</code> - 获得的控制点数（control points）</li>
                <li><code>energySpent</code> - 此次行动消耗的能量</li></ul>
            
        </td>
    </tr>
    <tr>
        <td><code>EVENT_EXIT</code></td>
        <td>
            一个 creep 移动到了其他房间。
            <ul>
                <li><code>room</code> - 目标房间的名称</li>
                <li><code>x</code>, <code>y</code> - creep 将要出现在其他房间的坐标位置</li></ul>
            
        </td>
    </tr>
    <tr>
        <td><code>EVENT_TRANSFER</code></td>
        <td>
            一个 link 执行了 <a href="https://docs.screeps.com/api/#StructureLink.transferEnergy" target="_blank" rel="noopener"><code>transferEnergy</code></a> 或者一个 creep 执行了 <a href="#Creep.transfer"><code>transfer</code></a> 或 <a href="#Creep.withdraw"><code>withdraw</code></a>。
            <ul>
                <li><code>targetId</code> - 目标对象 ID</li>
                <li><code>resourceType</code> - 被转移的资源类型</li>
                <li><code>amount</code> - 被转移的资源总量</li>
            </ul>
        </td>
    </tr>
</table>

<h2 id="Room.getPositionAt" class="api-property api-property--method  "><span class="api-property__name">getPositionAt</span><span class="api-property__args">(x, y)</span>
        <div class="api-property__cpu api-property__cpu--1" title="该方法具有较低的CPU开销。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> pos <span class="token operator">=</span> Game<span class="token punctuation">.</span>rooms<span class="token punctuation">.</span>sim<span class="token punctuation">.</span><span class="token function">getPositionAt</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> source <span class="token operator">=</span> pos<span class="token punctuation">.</span><span class="token function">findClosestByRange</span><span class="token punctuation">(</span><span class="token constant">FIND_SOURCES_ACTIVE</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>获取指定位置的 <a href="#RoomPosition">RoomPosition</a>  对象。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>x</code></td><td>number</td><td><p>X 坐标。</p>
</td>
</tr><tr><td><code>y</code></td><td>number</td><td><p>Y 坐标。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h3><p>一个
<a href="#RoomPosition">RoomPosition</a>
对象，如果无法获取则返回 null。</p>
<h2 id="Room.getTerrain" class="api-property api-property--method  "><span class="api-property__name">getTerrain</span><span class="api-property__args">()</span>
        <div class="api-property__cpu api-property__cpu--0" title="该方法的CPU开销很小。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> terrain <span class="token operator">=</span> Game<span class="token punctuation">.</span>rooms<span class="token punctuation">[</span><span class="token string">'W1N1'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">getTerrain</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">switch</span><span class="token punctuation">(</span>terrain<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token constant">TERRAIN_MASK_WALL</span><span class="token punctuation">:</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token constant">TERRAIN_MASK_SWAMP</span><span class="token punctuation">:</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">0</span><span class="token punctuation">:</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>获取一个 <a href="#Room-Terrain"><code>Room.Terrain</code></a> 对象，可以用它来快速访问房间内的静态地形数据。即使没有指定房间的视野，您依旧可以使用该方法访问它的地形数据，该方法适用于游戏世界中的所有房间。</p>
<h3 id="返回值-9"><a href="#返回值-9" class="headerlink" title="返回值"></a>返回值</h3><p>返回一个新的 <a href="#Room-Terrain"><code>Room.Terrain</code></a> 对象。</p>
<h2 id="Room.lookAt" class="api-property api-property--method  "><span class="api-property__name">lookAt</span><span class="api-property__args">(x, y)<br>(target)</span>
        <div class="api-property__cpu api-property__cpu--2" title="该方法的CPU开销中等。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> look <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">lookAt</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">;</span>
look<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">lookObject</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>lookObject<span class="token punctuation">.</span>type <span class="token operator">==</span> <span class="token constant">LOOK_CREEPS</span> <span class="token operator">&amp;&amp;</span>
       lookObject<span class="token punctuation">[</span><span class="token constant">LOOK_CREEPS</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">getActiveBodyparts</span><span class="token punctuation">(</span><span class="token constant">ATTACK</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        creep<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>lookObject<span class="token punctuation">.</span>creep<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>获取指定房间位置的对象数组。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>x</code></td><td>number</td><td><p>该房间中的 X 坐标。</p>
</td>
</tr><tr><td><code>y</code></td><td>number</td><td><p>该房间中的 Y 坐标。</p>
</td>
</tr><tr><td><code>target</code></td><td>object</td><td><p>可以是 <a href="#RoomPosition">RoomPosition</a> 对象或者任何包含 <a href="#RoomPosition">RoomPosition</a> 属性的对象。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-10"><a href="#返回值-10" class="headerlink" title="返回值"></a>返回值</h3><p>一个位于指定位置的对象数组，格式如下：</p>
<pre class="highlight javascript tab-javascript nosidebar"><code><span class="token punctuation">[</span>
    <span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'creep'</span><span class="token punctuation">,</span> creep<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'structure'</span><span class="token punctuation">,</span> structure<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token operator">...</span>
    <span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span> <span class="token punctuation">}</span>
<span class="token punctuation">]</span></code></pre><h2 id="Room.lookAtArea" class="api-property api-property--method  "><span class="api-property__name">lookAtArea</span><span class="api-property__args">(top, left, bottom, right, [asArray])</span>
        <div class="api-property__cpu api-property__cpu--2" title="该方法的CPU开销中等。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> look <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">lookAtArea</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>获取指定房间区域内的对象列表。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>top</code></td><td>number</td><td><p>区域顶部边界的 Y 坐标。</p>
</td>
</tr><tr><td><code>left</code></td><td>number</td><td><p>区域左侧边界的 X 坐标。</p>
</td>
</tr><tr><td><code>bottom</code></td><td>number</td><td><p>区域底部边界的 Y 坐标。</p>
</td>
</tr><tr><td><code>right</code></td><td>number</td><td><p>区域右侧边界的 X 坐标。</p>
</td>
</tr><tr><td><code>asArray (可选)</code></td><td>boolean</td><td><p>设为 true 来获得纯数组形式。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-11"><a href="#返回值-11" class="headerlink" title="返回值"></a>返回值</h3><p>如果 <code>asArray</code> 值为 false 或者未定义，则该方法以如下格式返回指定区域内的对象：</p>
<pre class="highlight javascript tab-javascript nosidebar"><code><span class="token comment">// 10,5,11,7</span>

<span class="token punctuation">{</span>
    <span class="token number">10</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token number">5</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'creep'</span><span class="token punctuation">,</span> creep<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token number">6</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token number">7</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span> <span class="token punctuation">}</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token number">11</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token number">5</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'plain'</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token number">6</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'structure'</span><span class="token punctuation">,</span> structure<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token number">7</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'wall'</span> <span class="token punctuation">}</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>如果 <code>asArray</code> 值为 true，则该方法以如下格式返回指定区域内的对象数组：</p>
<pre class="highlight javascript tab-javascript nosidebar"><code><span class="token punctuation">[</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">'creep'</span><span class="token punctuation">,</span> creep<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">6</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">7</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'plain'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">6</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">'structure'</span><span class="token punctuation">,</span> structure<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">6</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'swamp'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">7</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">'terrain'</span><span class="token punctuation">,</span> terrain<span class="token punctuation">:</span> <span class="token string">'wall'</span><span class="token punctuation">}</span>
<span class="token punctuation">]</span></code></pre><h2 id="Room.lookForAt" class="api-property api-property--method  "><span class="api-property__name">lookForAt</span><span class="api-property__args">(type, x, y)<br>(type, target)</span>
        <div class="api-property__cpu api-property__cpu--1" title="该方法具有较低的CPU开销。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> found <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">lookForAt</span><span class="token punctuation">(</span><span class="token constant">LOOK_CREEPS</span><span class="token punctuation">,</span> target<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>found<span class="token punctuation">.</span>length <span class="token operator">&amp;&amp;</span> found<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">getActiveBodyparts</span><span class="token punctuation">(</span><span class="token constant">ATTACK</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    creep<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>found<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>在指定位置查找指定类型的对象。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>type</code></td><td>string</td><td><p><code>LOOK_*</code> 常量之一。</p>
</td>
</tr><tr><td><code>x</code></td><td>number</td><td><p>该房间中的 X 坐标。</p>
</td>
</tr><tr><td><code>y</code></td><td>number</td><td><p>该房间中的 Y 坐标。</p>
</td>
</tr><tr><td><code>target</code></td><td>object</td><td><p>可以是 <a href="#RoomPosition">RoomPosition</a> 对象或者任何包含 <a href="#RoomPosition">RoomPosition</a> 属性的对象。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-12"><a href="#返回值-12" class="headerlink" title="返回值"></a>返回值</h3><p>在指定位置找到的指定类型的对象数组。</p>
<h2 id="Room.lookForAtArea" class="api-property api-property--method  "><span class="api-property__name">lookForAtArea</span><span class="api-property__args">(type, top, left, bottom, right, [asArray])</span>
        <div class="api-property__cpu api-property__cpu--1" title="该方法具有较低的CPU开销。"></div>
        </h2>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">const</span> look <span class="token operator">=</span> creep<span class="token punctuation">.</span>room<span class="token punctuation">.</span><span class="token function">lookForAtArea</span><span class="token punctuation">(</span><span class="token constant">LOOK_STRUCTURES</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在指定房间区域查找指定类型的对象列表。</p>
<table><thead><tr><th>parameter</th><th>type</th><th>description</th></tr></thead><tbody>
<tr><td><code>type</code></td><td>string</td><td><p><code>LOOK_*</code> 常量之一</p>
</td>
</tr><tr><td><code>top</code></td><td>number</td><td><p>区域顶部边界的 Y 坐标。</p>
</td>
</tr><tr><td><code>left</code></td><td>number</td><td><p>区域左侧边界的 X 坐标。</p>
</td>
</tr><tr><td><code>bottom</code></td><td>number</td><td><p>区域底部边界的 Y 坐标。</p>
</td>
</tr><tr><td><code>right</code></td><td>number</td><td><p>区域右侧边界的 X 坐标。</p>
</td>
</tr><tr><td><code>asArray (可选)</code></td><td>boolean</td><td><p>设为 true 来获得纯数组形式。</p>
</td>
</tr></tbody></table>

<h3 id="返回值-13"><a href="#返回值-13" class="headerlink" title="返回值"></a>返回值</h3><p>如果 <code>asArray</code> 值为 false 或者未定义，则该方法以如下格式返回指定区域内的对象：</p>
<pre class="highlight javascript tab-javascript nosidebar"><code><span class="token comment">// 10,5,11,7</span>

<span class="token punctuation">{</span>
    <span class="token number">10</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token number">5</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token number">6</span><span class="token punctuation">:</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span>
        <span class="token number">7</span><span class="token punctuation">:</span> <span class="token keyword">undefined</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token number">11</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token number">5</span><span class="token punctuation">:</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span>
        <span class="token number">6</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token number">7</span><span class="token punctuation">:</span> <span class="token keyword">undefined</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>如果 <code>asArray</code> 值为 true，则该方法以如下格式返回指定区域内的对象数组：</p>
<pre class="highlight javascript tab-javascript nosidebar"><code><span class="token punctuation">[</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> structure<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">6</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">,</span> structure<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>x<span class="token punctuation">:</span> <span class="token number">6</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">,</span> structure<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">]</span></code></pre>